package org.eae.eldf.service.module.impl;

import java.util.Date;
import java.util.List;
import org.eae.eldf.dao.module.IModuleRoleDao;
import org.eae.eldf.dao.module.IModuleRolePurviewDao;
import org.eae.eldf.dao.module.impl.ModuleRoleDaoImpl;
import org.eae.eldf.dao.module.impl.ModuleRolePurviewDaoImpl;
import org.eae.eldf.dao.portal.IPortalCompanyRoleDao;
import org.eae.eldf.dao.portal.IPortalCompanyRolePurviewDao;
import org.eae.eldf.dao.portal.impl.PortalCompanyRoleDaoImpl;
import org.eae.eldf.dao.portal.impl.PortalCompanyRolePurviewDaoImpl;
import org.eae.eldf.entity.module.ModuleRole;
import org.eae.eldf.entity.module.ModuleRolePurview;
import org.eae.eldf.entity.portal.PortalCompanyRole;
import org.eae.eldf.entity.portal.PortalCompanyRolePurview;
import org.eae.eldf.framework.persistence.Criteria;
import org.eae.eldf.framework.service.BaseServiceImpl;
import org.eae.eldf.framework.spring.SpringSecurityUtils;
import org.eae.eldf.service.module.ModuleRolePurviewService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class ModuleRolePurviewServiceImpl extends
		BaseServiceImpl<ModuleRolePurview> implements ModuleRolePurviewService {

	private IModuleRolePurviewDao moduleRolePurviewDao;
	private IModuleRoleDao moduleRoleDao;

	@Autowired
	public void setModuleRoleDao(ModuleRoleDaoImpl moduleRoleDao) {
		this.moduleRoleDao = moduleRoleDao;
	}

	@Autowired
	public void setModuleRolePurviewDao(
			ModuleRolePurviewDaoImpl moduleRolePurviewDao) {
		this.moduleRolePurviewDao = moduleRolePurviewDao;
		dao = moduleRolePurviewDao;
	}

	private IPortalCompanyRolePurviewDao portalCompanyRolePurviewDao;
	private IPortalCompanyRoleDao portalCompanyRoleDao;

	@Autowired
	public void setPortalCompanyRoleDao(
			PortalCompanyRoleDaoImpl portalCompanyRoleDao) {
		this.portalCompanyRoleDao = portalCompanyRoleDao;
	}

	@Autowired
	public void setPortalCompanyRolePurviewDao(
			PortalCompanyRolePurviewDaoImpl portalCompanyRolePurviewDao) {
		this.portalCompanyRolePurviewDao = portalCompanyRolePurviewDao;
	}
	
	

	/**
	 * {@inheritDoc} overridden:
	 * 
	 * @Date : 2016-6-27
	 * @see org.eae.eldf.service.module.ModuleRolePurviewService#rolePurviewSave(java.lang.Long,
	 *      java.lang.String[])
	 **/
	@Override
	public void rolePurviewSave(Long roleId, List<Long> purid) {
		ModuleRole mr = moduleRoleDao.get(ModuleRole.class, roleId);

		moduleRolePurviewDao.delete(Criteria.create(ModuleRolePurview.class)
				.where("moduleRoleId", new Object[] { roleId }));

		PortalCompanyRole pr = portalCompanyRoleDao.querySingleResult(Criteria
				.create(PortalCompanyRole.class)
				.where("companyCode", new Object[] { "system" })
				.and("roleName", new Object[] { mr.getRoleName() }));
		if(null!=pr){
			portalCompanyRolePurviewDao.delete(Criteria
					.create(PortalCompanyRolePurview.class)
					.where("companyCode", new Object[] { "system" })
					.and("portalCompanyRoleId",
							new Object[] { pr.getPortalCompanyRoleId() }));
		}

		for (Long pid : purid) {
			if(null!=pid){
				Long modulePurviewId = pid;
				Criteria criteria = Criteria
						.create(ModuleRolePurview.class)
						.set("moduleRoleId", roleId)
						.set("modulePurviewId", modulePurviewId)
						.set("lastUpdateTime", new Date())
						.set("lastUpdateUser",
								SpringSecurityUtils.getCurrentUserName())
						.set("createTime", new Date())
						.set("createUser", SpringSecurityUtils.getCurrentUserName());
				moduleRolePurviewDao.insert(criteria);

//				Criteria c = Criteria
//						.create(PortalCompanyRolePurview.class)
//						.set("portalCompanyRoleId", pr.getPortalCompanyRoleId())
//						.set("modulePurviewId", modulePurviewId)
//						.set("companyCode", "system")
//						.set("lastUpdateTime", new Date())
//						.set("lastUpdateUser",
//								SpringSecurityUtils.getCurrentUserName())
//						.set("createTime", new Date())
//						.set("createUser", SpringSecurityUtils.getCurrentUserName());
//				portalCompanyRolePurviewDao.insert(c);
			}
			

		}
	
	}
	
	
	@Override
	public void saveRolePurview(Long roleId, List<Long> purid) {
		//先删除角色对应的权限 sys_role_purview
		moduleRolePurviewDao.delete(Criteria.create(ModuleRolePurview.class).where("moduleRoleId", new Object[]{roleId}));
		if(null!=purid){
			for(Long pid:purid){
				ModuleRolePurview rp = new ModuleRolePurview();
				rp.setLastUpdateTime(new Date());
				rp.setLastUpdateUser(SpringSecurityUtils.getCurrentUserName());
				rp.setModulePurviewId(pid);
				rp.setModuleRoleId(roleId);
				moduleRolePurviewDao.insert(rp);
			}
		}
		
		
	}

	// @Override
	// //查询模块角色菜单树
	// public List<ModuleRolePurviewDto> queryModuleRolePurviewList(
	// List<ModuleRolePurviewDto> list, long moduleRoleId) {
	// return moduleRolePurviewDao.queryModuleRolePurviewList(list,
	// moduleRoleId);
	// }

}
